package com.yang.token.dao;

import com.yang.token.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

@Repository
public class UserDao {

    @Autowired
    DataSource dataSource;

    Connection conn;
    PreparedStatement pstmt;

    public List<User> queryAll(){
        ResultSet rs;
        List<User> userList = new ArrayList<>();

        try {
            conn = dataSource.getConnection();
            pstmt = conn.prepareStatement("SELECT * FROM user");
            rs = pstmt.executeQuery();
            while (rs.next()) {
                int uid = rs.getInt("uid");
                String userName = rs.getString("user_name");
                String passWord = rs.getString("pass_word");
                int salary = rs.getInt("salary");
                User u = new User(uid, userName, passWord, salary);
                userList.add(u);
            }
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userList;
    }

    public User findUserById(int id){
        ResultSet rs;
        User u = new User();

        try {
            conn = dataSource.getConnection();
            pstmt = conn.prepareStatement("SELECT * FROM user where uid = ?");
            pstmt.setInt(1, id);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                int uid = rs.getInt("uid");
                u.setUid(uid);
                String userName = rs.getString("user_name");
                u.setUserName(userName);
                String passWord = rs.getString("pass_word");
                u.setPassWord(passWord);
                int salary = rs.getInt("salary");
                u.setSalary(salary);
            }
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return u;
    }

    public int updateUser(User user){

        int i = 0;
        try {
            conn = dataSource.getConnection();
            pstmt = conn.prepareStatement("update user set user_name=?, pass_word=?, salary=? where uid=?");
            pstmt.setString(1, user.getUserName());
            pstmt.setString(2, user.getPassWord());
            pstmt.setInt(3, user.getSalary());
            pstmt.setInt(4, user.getUid());
            i = pstmt.executeUpdate();
            System.out.println(i);

            pstmt.close();
            conn.close();

        }catch (Exception e){
            e.printStackTrace();
        }

        return i;
    }

    public int deleteUserById(int id){

        int i = 0;
        try {
            conn = dataSource.getConnection();
            pstmt = conn.prepareStatement("delete from user where uid= ?");
            pstmt.setInt(1, id);

            i = pstmt.executeUpdate();
            System.out.println(i);

            pstmt.close();
            conn.close();

        }catch (Exception e){
            e.printStackTrace();
        }

        return i;
    }

}
